package com.controller;

import com.constant.Constant;
import com.entity.CourseType;
import com.entity.User;
import com.exception.ServiceException;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.mapper.MenuMapper;
import com.service.LoginService;
import com.util.AjaxResult;
import com.util.CommonUtil;
import com.util.RandomValidateCode;
import com.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * name:LoginController
 * date:2021/6/29
 * Author:gxh
 * description:
 * version:
 */
@RestController
public class LoginController {
    @Autowired
    LoginService loginService;

    @Autowired
    MenuMapper menuMapper;

    /**
     * 判断用户是否登陆，可以自行修改
     *
     * @param
     * @return
     */
    @RequestMapping("/netClass/getUser")
    public boolean getSessionUser(HttpServletRequest request) {
//        Object user = session.getAttribute("user");
        Object user = request.getSession().getAttribute("user");
        System.out.println(user);
        if (user != null) {
            return true;
        }
        return false;
    }

    /**
     * 用户登录
     */

    @RequestMapping("/user/loginFront")
    public AjaxResult loginUser(User u, String login_name, String password, HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        List<User> users = loginService.LoginISNameAndPassWord(login_name, password);
        HttpSession session = request.getSession();
        boolean flag = false;
        boolean flagp = false;
        for (User s : users) {
            String login_name1 = s.getLogin_name();
            String password2 = s.getPassword();
            if (login_name1.equals(login_name)) {
                flag = true;
            }
            if (password2.equals(password) && flag == true) {
                flagp = true;
            }
        }
        if (flagp == false) {
            ajaxResult.setMsg("密码错误");
            return ajaxResult;
        }
        if (flag == false) {
            ajaxResult.setMsg("用户名错误");
            return ajaxResult;
        }
        if (flag == true && flagp == true) {
            loginService.upLoginDateS(login_name);////方法执行更新时间
            session.setAttribute("user", users.get(0));
        }
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    //后台验证码
    @RequestMapping("/user/getCode.do")
    public void getCode(HttpServletRequest request, HttpServletResponse response) throws Exception {

        response.setContentType("image/jpeg");
        response.setHeader("pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            randomValidateCode.getRandcode(request, response);//输出图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
//        AjaxResult ajaxResult = new AjaxResult();
//        //验证码显示
//        CommonUtil.MyImage m = CommonUtil.getImage(null, 4, true, true);
//        request.getSession().setAttribute("code", m.getCode());
//        ServletOutputStream responseOutputStream = response.getOutputStream();
//        // 输出图象到页面
//        ImageIO.write(m.getImage(), "JPEG", responseOutputStream);
//        // 以下关闭输入流！
//        responseOutputStream.flush();
//        responseOutputStream.close();
//        //验证码错误
//        boolean flag = false;
//        String localCode = (String) request.getSession().getAttribute("code");
//        if (localCode == null || !localCode.equals(code)) {
//            ajaxResult.setSuccess(false);
//            ajaxResult.setMsg("验证码错误");
//        }
//        return AjaxResult.isOk();
    }

//后台验证管理员登录 并将USER对象跟全部课程类别表存入Session作用域
    @RequestMapping("/user/loginBack.do")
    public AjaxResult loginBack(HttpServletRequest request,String password,String login_name,User user, String code, HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();
        String randomcode_key = (String)request.getSession().getAttribute("randomcode_key");
        boolean flag=false;
        boolean flagp=false;
        boolean flags=false;
        List<User> users = loginService.LoginISNameAndPassWord(login_name, password);
        System.out.println(users+"==============");
        System.out.println(randomcode_key+ "111111111111111");
        for(User s:users){
            String login_name1 = s.getLogin_name();
            String password2 = s.getPassword();
            if(login_name1.equals(login_name)){
                System.out.println("2222222222222");
                flag=true;
            }
            if(password2.equals(password) && flag==true){
                System.out.println("33333333333333333333333");
                flagp=true;
            }
            if(password2.equals(password) && flag==true&&randomcode_key.equals(code)){
                System.out.println("4444444444444444");
                flags=true;
                ajaxResult.setSuccess(true);
            }
        }
        if(flagp==false){
            ajaxResult.setMsg("密码错误");
            return ajaxResult;
        }
        if(flag==false){
            ajaxResult.setMsg("用户名错误");
            return ajaxResult;
        }
        if (randomcode_key == null || !randomcode_key.equals(code)) {

            ajaxResult.setMsg("验证码错误");
//            ajaxResult.setSuccess(false);
            return ajaxResult;
        }

        if (flag==true && flagp==true){
            session.setAttribute("user",users.get(0));
            session.setAttribute("user",users.get(1));
        }

        return ajaxResult;
    }}


